#region File Description
//-----------------------------------------------------------------------------
// SpriteSheet.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace Ramparts
{

    /// <summary>
    /// Stores entries for individual sprites on a single texture.
    /// </summary>
    public class SpriteSheet
    {
        #region Fields
        private Texture2D texture;
        private Dictionary<int, Rectangle> spriteDefinitions;
        #endregion

        #region Constructors
        /// <summary>
        /// Create a new Sprite Sheet
        /// </summary>
        public SpriteSheet(Texture2D sheetTexture)
        {
            texture = sheetTexture;
            spriteDefinitions = new Dictionary<int, Rectangle>();
        }
        #endregion

        #region Methods
        /// <summary>
        /// Add a source sprite for fast retrieval
        /// </summary>
        public void AddSourceSprite(int key, Rectangle rect)
        {
            spriteDefinitions.Add(key, rect);
        }

        /* Helper Function to 
         * Draw an individual sprite sheet
         */
        public void drawTextureByIndex(int index, Vector2 position, SpriteBatch spriteBatch)
        {
             spriteBatch.Draw(
                  Texture, position ,this.GetRectangle(index),
                            Color.White,
                        0, position, 1, SpriteEffects.None, 0.0f);
      
        }

        /* Helper Function to 
        * Draw an individual sprite sheet
        * from x,y
        */
        public void drawTextureByIndex(int index, int x, int y, SpriteBatch spriteBatch)
        {
            this.drawTextureByIndex(index, new Vector2(x, y), spriteBatch);

        }

        public void BatchAddSourceSprite(int cellWidth, int cellHeight)
        {

            int index = 0;
                for (int y = 0; y < this.texture.Height; y += cellHeight)
                {
                    for (int x = 0; x < this.texture.Width; x += cellWidth)
                    {
           
                    spriteDefinitions.Add(index, new Rectangle(x,y,cellWidth,cellHeight));
                    index++;
                }
            }
        }

        /// <summary>
        /// Get the source sprite texture
        /// </summary>
        public Texture2D Texture
        {
            get
            {
                return texture;
            }
        }

        /// <summary>
        /// Get the rectangle that defines the source sprite
        /// on the sheet.
        /// </summary>
        public Rectangle this[int i]
        {
            get
            {
                return spriteDefinitions[i];
            }
        }

        /// <summary>
        /// A faster lookup using refs to avoid stack copies.
        /// </summary>
        public void GetRectangle(ref int i, out Rectangle rect)
        {
            rect = spriteDefinitions[i];
        }

        public Rectangle GetRectangle(int i)
        {
            return spriteDefinitions[i];
        }
        

        
        #endregion
    }

}